﻿using FastCloud.Base;
using FastCloud.Extend.DB;
using FastCloud.Extend.Entity;
using FastIOC.Attribute;
using FastORM.Entity;
using FastUtil.Convert;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FastCloud.Controllers.Extend
{
    [Route("frame/extend/elementright")]
    public class ElementRIghtController : BaseController
    {
        [AutoWired]
        private DB_Element DB_Element;
        [AutoWired]
        private DB_ElementRight DB_ElementRight;

        [Route("list")]
        public IActionResult List()
        {
            return View("~/Views/Extend/ElementRight/ElementRight_List.cshtml");
        }

        [Route("edit")]
        public IActionResult Edit()
        {
            return View("~/Views/Extend/ElementRight/ElementRight_Edit.cshtml");
        }

        [Route("oplist"), HttpPost]
        public IActionResult OpList()
        {
            string ElementName = ValueConvert.ConvertToString(GetFormParam("ElementName"));
            string sql = "1=1";
            List<ParamEntity> ParamList = new List<ParamEntity>();
            if (!string.IsNullOrEmpty(ElementName))
            {
                sql += " and ElementName like @ElementName";
                ParamList.Add(new ParamEntity() { ParamName = "ElementName", ParamValue = "%" + ElementName + "%" });
            }

            return Result.OK(DB_Element.SelectPage("*", sql, "SortNum desc", ParamList));
        }

        [Route("getElementRightData"), HttpPost]
        public IActionResult GetElementRightData()
        {
            Hashtable result = new Hashtable();
            string ElementGuid = ValueConvert.ConvertToString(GetFormParam("ElementGuid"));
            List<Frame_ElementRight> RightList = DB_ElementRight.SelectAll().Where(a => a.ElementGuid == ElementGuid).ToList();
            List<string> OURightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.部门).Select(a => a.RightGuid).ToList();
            List<string> UserRightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.人员).Select(a => a.RightGuid).ToList();
            List<string> RoleRightList = RightList.Where(a => a.RightType == (int)FastCloud.Base.Enum.ORG.RightType.角色).Select(a => a.RightGuid).ToList();
            result.Add("OUGuidList", string.Join(";", OURightList.ToArray()));
            result.Add("UserGuidList", string.Join(";", UserRightList.ToArray()));
            result.Add("RoleGuidList", string.Join(";", RoleRightList.ToArray()));
            return Result.OK(result);
        }


        [Route("updateElementRightData"), HttpPost]
        public IActionResult UpdateElementRightData()
        {
            string ElementGuid = ValueConvert.ConvertToString(GetFormParam("ElementGuid"));
            string[] OUGuidArray = ValueConvert.ConvertToString(GetFormParam("OUGuidList")).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] UserGuidArray = ValueConvert.ConvertToString(GetFormParam("UserGuidlist")).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] RoleGuidArray = ValueConvert.ConvertToString(GetFormParam("RoleGuidList")).Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            DB_ElementRight.DeleteByElementGuid(ElementGuid);
            foreach (string OUGuid in OUGuidArray)
            {
                Frame_ElementRight Right = new Frame_ElementRight();
                Right.ElementGuid = ElementGuid;
                Right.RightGuid = OUGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.部门;
                DB_ElementRight.Insert(Right);
            }
            foreach (string UserGuid in UserGuidArray)
            {
                Frame_ElementRight Right = new Frame_ElementRight();
                Right.ElementGuid = ElementGuid;
                Right.RightGuid = UserGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.人员;
                DB_ElementRight.Insert(Right);
            }
            foreach (string RoleGuid in RoleGuidArray)
            {
                Frame_ElementRight Right = new Frame_ElementRight();
                Right.ElementGuid = ElementGuid;
                Right.RightGuid = RoleGuid;
                Right.RightType = (int)FastCloud.Base.Enum.ORG.RightType.角色;
                DB_ElementRight.Insert(Right);
            }
            return Result.OK();
        }
    }
}
